package com.gxa.hotel.admin.advice;


import com.gxa.hotel.core.common.JsonResult;
import com.gxa.hotel.core.exception.BusinessException;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authc.AuthenticationException;
import org.springframework.validation.Errors;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import javax.validation.ConstraintViolation;
import javax.validation.ConstraintViolationException;
import java.util.Set;

@RestControllerAdvice
//@Log4j2
@Slf4j
public class GlobalExceptionHandler {
    // 处理第一种校验异常: 将异常信息拼接,返回给前端显示
    @ExceptionHandler({AuthenticationException.class})
    public JsonResult handleBindException(Errors errors){
        System.out.println(1);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("0");
        StringBuilder stringBuilder = new StringBuilder();
        errors.getAllErrors().forEach(error->{
            stringBuilder.append(error.getDefaultMessage()+",");
        });
        jsonResult.setMsg(stringBuilder.toString());
        return jsonResult;
    }
    // 处理第二种校验异常: 将异常信息拼接,返回给前端显示
    @ExceptionHandler(ConstraintViolationException.class)
    public JsonResult
    handleConstraintViolationException(ConstraintViolationException exception) {
        System.out.println(2);
        StringBuilder errorInfo = new StringBuilder();
        String errorMessage ;
        Set<ConstraintViolation<?>> violations =
                exception.getConstraintViolations();
        for (ConstraintViolation<?> item : violations) {
            errorInfo.append(item.getMessage()).append(",");

        }
        errorMessage = errorInfo.toString().substring(0,
                errorInfo.toString().length()-1);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("0");
        jsonResult.setMsg(errorMessage);
        return jsonResult;
    }
    // 处理业务异常: 将异常信息返回给前端显示
    @ExceptionHandler(BusinessException.class)
    public JsonResult
    handleBusinessException(BusinessException exception) {
        System.out.println(3);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("0");
        jsonResult.setMsg(exception.getMessage());
        return jsonResult;
    }
// 处理系统异常: 将异常信息返回给前端打哈哈,系统后台记录日志
    @ExceptionHandler(Exception.class)
    public JsonResult handleException(Exception e) {
// 把系统异常日志记录
        log.info("-----------------begin------------------ -----"); //分割线方便阅读
                log.info(String.valueOf(e)); // 先记录异常对象的toString状态: 包括异常 类型和异常原因
// 再遍历栈中的异常轨迹: 采用遍历方式
        StackTraceElement[] stackTrace =
                e.getStackTrace();
        for (StackTraceElement stackTraceElement :stackTrace) {
            log.info(String.valueOf(stackTraceElement));
        }
        log.info("-------------------end------------------ ---");//分割线方便阅读
                JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("0");
        jsonResult.setMsg("系统开小差了,请稍后再试");
        return jsonResult;
    }


}
